package com.web.servlet;

import com.web.daoimpl.StudentDaoImpl;
import com.web.entity.Student;
import com.web.service.StudentService;
import com.web.serviceimpl.StudentServiceImpl;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 学生注册
 *
 * @author 17612
 */
@WebServlet("/ins")
public class NewSRegister extends HttpServlet {
    StudentServiceImpl service = new StudentServiceImpl();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        
        req.setCharacterEncoding("utf-8");
        
        String sid = req.getParameter("Sid");
        String sname = req.getParameter("Sname");
        String spwd = req.getParameter("Spwd");
        String sgender = req.getParameter("Sgender");
        String classid = req.getParameter("ClassID");

        int flag;//用于验证是否已有该学生,保证唯一性
        String tip = null;//作为注册是否成功的返回信息
        Student student = new Student(sid,sname,spwd,sgender,classid);
        System.out.println(student);
        flag = service.RegistrationVerification(student);
        switch (flag) {
            case 0://注册成功
                service.insertNewStudent(student);
                break;
            case 1://Sid已存在
                tip = "您的id已被注册，请返回登录或者重新输入继续注册";
                break;
        }

        if (null != tip) {
            req.setAttribute("tip", tip);
            req.getRequestDispatcher("/no_register.jsp").forward(req, resp);//注册失败,返回首页看失败信息
        } else {
            req.getRequestDispatcher("S_Register.jsp").forward(req, resp);//注册成功，返回注册成功页面
        }
    }
}
